/**
 * 程序名称：SerializationDemo.java
 * 程序主题：实现对象的序列化和反序列化 
 * 程序说明：该程序由实例化一个MyClass类的对象开始，该对象有三个实例变量，
 *         类型分别为String、int、double，是希望存储和恢复的信息。
 * @author MIAO
 * Created on 2011-10-20
 * For more information, please visit:ismiao.com
 */
package com.ismiao.java.Serializable;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializationDemo {
	public static void main(String args[]) {
		// Object serialization
		try {
			MyClass object1 = new MyClass("Hello", -7, 2.7e10);
			System.out.println("object1:" + object1);
			FileOutputStream fos = new FileOutputStream("serial");
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(object1);
			oos.flush();
			oos.close();
		} catch (Exception e) {
			System.out.println("Exception during serialization:" + e);
			System.exit(0);
		}

		// Object deserialization
		try {
			MyClass object2;
			FileInputStream fis = new FileInputStream("serial");
			ObjectInputStream ois = new ObjectInputStream(fis);
			object2 = (MyClass) ois.readObject();
			ois.close();
			System.out.println("object2:" + object2);
		} catch (Exception e) {
			System.out.println("Exception during deserialization:" + e);
			System.exit(0);
		}
	}
}
